class Comment < ActiveRecord::Base
  
  COMMENTS_PER_PAGE=10
  RECENT_COMMENT_LIMIT=5
  
  validates_presence_of :name ,:message=>'不能为空！' 
  validates_presence_of :email ,:message=>'不能为空！' 
  validates_presence_of :content ,:message=>'不能为空！' 
  
  belongs_to :resource, :polymorphic=>true, :counter_cache=>true
  
  def self.paginate_by(resource_type,resource_id,page)
    Comment.paginate(:per_page=>COMMENTS_PER_PAGE,
      :page=>page,
      :conditions=>["resource_type=? and resource_id=?",resource_type,resource_id],
      :order=>'created_at DESC')
  end
  
  def self.recent(type)
    Comment.find(:all,
      :conditions=>['resource_type=?',type],
      :limit=>RECENT_COMMENT_LIMIT)
  end
  
end
